package com.mall.weapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.weapp.mapper.BrandMapper;
import com.mall.weapp.mapper.ProductMapper;
import com.mall.weapp.model.entity.Brand;
import com.mall.weapp.model.entity.Product;
import com.mall.weapp.model.vo.BrandVo;
import com.mall.weapp.model.vo.ProductListVo;
import com.mall.weapp.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<BrandVo> getBrandList() {
        List<Brand> brands = brandMapper.selectList(null);
        ArrayList<BrandVo> brandList = new ArrayList<>();
        for (Brand brand : brands) {
            brandList.add(new BrandVo(brand));
        }
        return brandList;
    }

    @Override
    public List<ProductListVo> getProductByBrandId(long id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getStatus, 1);
        wrapper.eq(Product::getBrandId, id);
        List<Product> products = productMapper.selectList(wrapper);
        List<ProductListVo> productList = new ArrayList<>();
        for (Product product : products) {
            productList.add(new ProductListVo(product));
        }
        return productList;
    }
}
